﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AnLe.Models;

namespace AnLe.Controllers
{
    public class ProjectsController : Controller
    {
        //
        // GET: /Projects/

        public ActionResult Index()
        {
            return View();
        }

        // User control : Get List top Projects(9 records)
        public ActionResult ListTopProjects()
        {
            List<Project> listPro = ProjectsModels.getTopProject(9);
            return View(listPro);
        }
        //View Detail by ID

        public ActionResult Details(int id)
        {
            Project pro = ProjectsModels.getProjectByID(id);
            return View();
        }

        public ActionResult AddProject()
        {
            return View();
        }
               
        [HttpPost]
        public ActionResult addProject(FormCollection formValue)
        {
            try
            {
                Project pro = new Project();
                pro.ProName = formValue["proName"];
                pro.Introduction = formValue["intro"];
                pro.ProImg = formValue["proImg"];
                pro.CateID = int.Parse("2");
                pro.Author = formValue["author"];
                pro.FinishedTime = formValue["link"];
                pro.FinishedTime = formValue["partner"];
                pro.FinishedTime = formValue["finished"];
                pro.Discount = formValue["discount"];
                pro.CreateDate = DateTime.Now;
                if (ProjectsModels.InsertPro(pro))
                {
                    ViewData["mess"] = "Add new book information is successful.";
                }
                else { ViewData["mess"] = "Add new book information is UNSUCESSFUL."; }
                return View(pro);
            }
            catch
            {
                return View();
            }
        }
      
        //
        // GET: /Projects/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Projects/Edit/5

        [HttpPost]
        public ActionResult UpdateProducts(int id, FormCollection collection)
        {
            try
            {
                Project pro = new Project();
                pro.ProID = id;
                pro.ProName = collection["ProName"].ToString();
                pro.Introduction = collection["Introduction"].ToString();
                pro.ProImg = collection["ProImg"].ToString();
                pro.CateID = int.Parse(collection["ddlCategory"]);
                pro.Author = collection["Author"].ToString();
                pro.Discount = collection["Discount"].ToString();
                if (ProjectsModels.UpdatePro(pro))
                {
                    ViewData["mess"] = "Update data is successful.";
                }
                else { ViewData["mess"] = "Update data is UNSUCESSFUL."; }
                //ViewData["listCate"] = new SelectList(CategoryModels.getAllCategory(), "CateID", "CateName", pro.CateID);
                return View(pro);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Projects/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Projects/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
